package com.tensquare.base.service;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.repository.LabelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: tensquare_parent
 * @description: 标签服务层
 * @author: NieMiao
 * @create: 2018-12-13 15:50
 **/
@Service
@Transactional
public class LabelService {
    @Autowired
    private LabelRepository labelRepository;
    @Autowired
    private IdWorker idWorker;
    public List<Label> findAll(){
        return labelRepository.findAll();
    }

    public Label findById(String id) {
        return labelRepository.findById(id).get();
    }
    public void save(Label label){
        label.setId(idWorker.nextId()+"");
        labelRepository.save(label);
    }
    public void update(Label label){
        labelRepository.save(label);
    }
    public void delete(String id) {
        labelRepository.deleteById(id);
    }

    public List<Label> searchList(Label label){
        return labelRepository.findAll(new Specification<Label>(){
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if(label.getLabelname()!=null && !"".equals(label.getLabelname())){
                    Predicate predicate = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(predicate);
                }
                if(label.getState()!=null && !"".equals(label.getState())){
                    Predicate predicate = cb.equal(root.get("state").as(String.class), label.getState());
                    list.add(predicate);
                }
                Predicate[] parr = new Predicate[list.size()];
                //把集合中的属性复制到数组中
                parr = list.toArray(parr);
                return cb.and(parr);
            }
        });
    }
    public Page<Label> searchPage(Label label, Integer page, Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return labelRepository.findAll(new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list= new ArrayList<Predicate>();
                if (!StringUtils.isEmpty(label.getLabelname())) {
                    Predicate labelname = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(labelname);
                }
                if (!StringUtils.isEmpty(label.getState())) {
                    Predicate labelname = cb.equal(root.get("state").as(String.class), label.getState());
                    list.add(labelname);
                }
                Predicate[] prr = new Predicate[list.size()];
                prr = list.toArray(prr);
                return cb.and(prr);
            }
        },pageable);
    }
}
